/* 
描述：定义标签管理模块的相关后端接口 
完成人：何艺珲
完成时间: 2025/04/26
*/
package com.jiesenhe.qacommunity.controller;


import com.jiesenhe.qacommunity.pojo.Question;
import com.jiesenhe.qacommunity.pojo.QuestionTag;
import com.jiesenhe.qacommunity.pojo.Tag;
import com.jiesenhe.qacommunity.service.TagService;
import com.jiesenhe.qacommunity.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    TagService tagService;
    @GetMapping("/getTagList")
    public ResponseEntity<Object> getTagList(Tag tag)
    {
        List<Tag> list = tagService.getTagList(tag);
        return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
            {
                put("code", (list != null && !list.isEmpty()) ? 1 : 0);
                put("msg", (list != null && !list.isEmpty()) ? "getTagList success" : "getTagList failed");
                put("data", list);
            }
        });
    }
    @PostMapping("/insertTag")
    public ResponseEntity<Object> insertTag(@RequestBody Tag tag, HttpServletRequest request)
    {
        String token = request.getHeader("Token");
        if (JwtUtil.checkToken(token) && JwtUtil.checkAdmin(token)){
            int result = tagService.insertTag(tag);
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", result != 0 ? 1 : 0);
                    put("msg", result != 0 ? "insertTag success" : "insertTag failed");
                    put("data", null);
                }
            });
        }else{
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed or not admin");
                    put("data", null);
                }
            });
        }
    }
    @GetMapping("/getTagQuestion")
    public ResponseEntity<Object> getTagQuestion(Question question) {
        int tagId = question.getId();
        List<Question> list = tagService.getTagQuestion(tagId);
        return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
            {
                put("code", (list != null && !list.isEmpty()) ? 1 : 0);
                put("msg", (list != null && !list.isEmpty()) ? "getTagQuestion success" : "getTagQuestion failed");
                put("data", list);
            }
        });
    }
    @DeleteMapping("/deleteTag")
    public ResponseEntity<Object> deleteTag(@RequestBody Tag tag, HttpServletRequest request) {
        String token = request.getHeader("Token");
        if (JwtUtil.checkToken(token) && JwtUtil.checkAdmin(token)) {
            int result = tagService.deleteTag(tag.getId());
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", result != 0  ? 1 : 0);
                    put("msg", result != 0  ? "deleteTag success" : "deleteTag failed");
                    put("data", null);
                }
            });
        } else {
            return ResponseEntity.ok(new java.util.HashMap<String, Object>() {
                {
                    put("code", 0);
                    put("msg", "token check failed or not admin");
                    put("data", null);
                }
            });
        }
    }
}
